package cn.edu.zafu.bigdata.zqt.studentsystem.utils;

import cn.edu.zafu.bigdata.zqt.studentsystem.cli.StudentManagerSystem;
import cn.edu.zafu.bigdata.zqt.studentsystem.constant.StudentConst;
import cn.edu.zafu.bigdata.zqt.studentsystem.entity.Student;

import java.util.Optional;
import java.util.Scanner;

/**
 * 输入工具类
 *
 * @author zqt
 */
public class InputUtils {

    /**
     * 获取学号
     *
     * @param isNew 是否为新建学生
     * @return 学号
     */
    public static String readId(boolean isNew) {
        String id;
        while (true) {
            System.out.println("请输入学号(10位数字)");
            id = StudentManagerSystem.scanner.nextLine();
            if (!id.matches(StudentConst.ID_REGEX)) {
                System.err.println("学号格式错误，请重新输入");
                continue;
            }
            if (isNew) {
                String finalId = id;
                Optional<Student> first = StudentManagerSystem
                        .studentList
                        .stream()
                        .filter(student -> student.getId().equals(finalId))
                        .findFirst();
                if (first.isPresent()) {
                    System.err.println("该学号已存在，请重新输入");
                    continue;
                }
            }
            return id;
        }
    }

    /**
     * 获取姓名
     *
     * @return 姓名
     */
    public static String readName() {
        String name;
        while (true) {
            System.out.println("请输入姓名(2-10位中文字符)");
            name = StudentManagerSystem.scanner.nextLine();
            if (!name.matches(StudentConst.NAME_REGEX)) {
                System.err.println("姓名格式错误，请重新输入");
                continue;
            }
            return name;
        }
    }

    /**
     * 获取电话
     *
     * @return 电话
     */
    public static String readPhone() {
        String phone;
        while (true) {
            System.out.println("请输入电话(11位数字)");
            phone = StudentManagerSystem.scanner.nextLine();
            if (!phone.matches(StudentConst.PHONE_REGEX)) {
                System.err.println("电话格式错误，请重新输入");
                continue;
            }
            return phone;
        }
    }

    /**
     * 读取学生对象并允许退出
     *
     * @return 学生对象
     */
    public static Student findStudentById() {
        Scanner scanner = StudentManagerSystem.scanner;
        String id;
        while (true) {
            System.out.print("请输入学生 10位数字的ID (或输入 QUIT 退出)：");
            id = scanner.nextLine();

            if ("QUIT".equalsIgnoreCase(id)) {
                System.err.println("退出当前操作。");
                return null;
            }
            if (!id.matches(StudentConst.ID_REGEX)) {
                System.err.println("学号格式不正确，请重新输入！");
            } else {
                break;
            }
        }
        String finalId = id;
        Optional<Student> first = StudentManagerSystem
                .studentList
                .stream()
                .filter(student -> student.getId().equals(finalId))
                .findFirst();
        if (first.isEmpty()) {
            System.err.println("未找到该学生信息！");
            return null;
        } else {
            return first.get();
        }
    }

    public static boolean confirm() {
        System.out.print("确认操作？(Y/N)：");
        String confirm = StudentManagerSystem.scanner.nextLine();
        if ("Y".equalsIgnoreCase(confirm)) {
            return true;
        } else {
            System.out.println("操作已取消。");
            return false;
        }
    }
}
